//
//  AffirmOrderHeaderView.m
//  Yanwei
//
//  Created by 刘文 on 17/5/9.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "AffirmOrderHeaderView.h"
#import "NSString+Util.h"

@interface AffirmOrderHeaderView ()

@property (strong, nonatomic) UIImageView * topImageView;
@property (strong, nonatomic) UILabel * nameLabel;
@property (strong, nonatomic) UILabel * phoneLabel;
@property (strong, nonatomic) UILabel * addressLabel;
@property (strong, nonatomic) UILabel * arrowLabel;
@property (strong, nonatomic) UILabel * defaultMsgLabel;
@property (strong, nonatomic) UIView * bgView;

@end

@implementation AffirmOrderHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = Color244;
        self.frame = CGRectMake(0, 0, ScreenWidth, 87);
    }
    return self;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self).offset(-10);
        }];
    }
    return _bgView;
}

- (void)setModel:(StoreAddressModel *)model {
    _model = model;
    self.arrowLabel.hidden = NO;
    self.topImageView.hidden = NO;
    if (!model) {
        self.defaultMsgLabel.hidden = NO;
        self.nameLabel.text = @"";
        self.phoneLabel.text = @"";
        self.addressLabel.text = @"";
    } else {
        self.defaultMsgLabel.hidden = YES;
        self.nameLabel.text = [NSString backDefaultIfNilWithString:model.name defaultString:@"未知"];
        self.phoneLabel.text = model.phone;
        self.addressLabel.text = [NSString stringWithFormat:@"%@%@", model.areaName, model.address];
    }
}

- (UIImageView *)topImageView {
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc] init];
        _topImageView.image = [UIImage imageNamed:@"dd_fg"];
        [self.bgView addSubview:_topImageView];
        [_topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.bgView);
            make.height.mas_equalTo(5);
        }];
    }
    return _topImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = APPFONT(16);
        _nameLabel.textColor = Color34;
        [self.bgView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.topImageView).with.offset(18);
            make.top.equalTo(self.topImageView).with.offset(18);
        }];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [UILabel new];
        _nameLabel.font = APPFONT(16);
        _nameLabel.textColor = Color34;
        [self.bgView addSubview:_phoneLabel];
        [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel.mas_right).with.offset(10);
            make.top.equalTo(self.nameLabel);
        }];
    }
    return _phoneLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [UILabel new];
        _addressLabel.font = APPFONT(14);
        _addressLabel.textColor = Color140;
        _addressLabel.numberOfLines = 2;
        _addressLabel.minimumScaleFactor = 10;
        [self.bgView addSubview:_addressLabel];
        [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel);
            make.top.equalTo(self.nameLabel.mas_bottom).with.offset(8);
            make.right.lessThanOrEqualTo(self.arrowLabel.mas_left).with.offset(-10);
        }];
    }
    return _addressLabel;
}

- (UILabel *)arrowLabel {
    if (!_arrowLabel) {
        _arrowLabel = [UILabel new];
        _arrowLabel.font = ICONFONT(16);
        _arrowLabel.textColor = Color100;
        _arrowLabel.text = ARROW_RIGHT;
        [self.bgView addSubview:_arrowLabel];
        [_arrowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgView).with.offset(-15);
            make.centerY.equalTo(self.bgView).with.offset(2.5);
        }];
    }
    return _arrowLabel;
}

- (UILabel *)defaultMsgLabel {
    if (!_defaultMsgLabel) {
        _defaultMsgLabel = [UILabel new];
        _defaultMsgLabel.font = APPFONT(16);
        _defaultMsgLabel.textColor = Color34;
        _defaultMsgLabel.text = @"请填写您的收货地址";
        [self.bgView addSubview:_defaultMsgLabel];
        [_defaultMsgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).with.offset(15);
            make.top.and.bottom.equalTo(self.bgView);
        }];
    }
    return _defaultMsgLabel;
}

@end
